<template>
  <div class="permissions-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="handleOpen(1, '0')">添加权限</el-button>
        </div>
        <el-table border :data="permissionList" row-key="id" default-expand-all>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="discription" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" size="small" type="text" @click="handleOpen(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="handleOpenEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="handleDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog title="新增权限(点)" :visible="showAddDialog" @close="handleAddClose">
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="permissionForm.name" style="width: 80%" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="permissionForm.code" style="width: 80%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="permissionForm.description" style="width: 80%" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permissionForm.enVisiable"
              active-color="#409EFF"
              inactive-color="#ccc"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleAddClose">取消</el-button>
          <el-button type="primary" @click="addPermissionForm">确定</el-button>
        </template>
      </el-dialog>
      <!-- 编辑弹框 -->
      <el-dialog title="编辑权限" :visible="showEditDialog" @close="handleEditClose">
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="permissionForm.name" style="width: 80%" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="permissionForm.code" style="width: 80%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="permissionForm.description" style="width: 80%" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permissionForm.enVisiable"
              active-color="#409EFF"
              inactive-color="#ccc"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleEditClose">取消</el-button>
          <el-button type="primary" @click="updataPermissionForm">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, addPermissionFormAPI, getPermissionDetailAPI, updatePermissionFormAPI, delPermissionAPI } from '@/api/permissions'
import { listToTree } from '@/utils/shift'
export default {
  name: 'Permission',
  data() {
    return {
      // 数据表格
      permissionList: [],
      showAddDialog: false,
      // 权限表单
      permissionForm: {
        name: '',
        code: '',
        description: '',
        enVisiable: '0'
      },
      showEditDialog: false
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    // 新增权限弹框打开时
    handleOpen(type, pid) {
      this.showAddDialog = true
      // 保存
      // type：1 =>页面访问权 2 => 按钮操作权
      // pid：父级id， '0'：表示页面访问权，否则就是按钮操作权
      this.type = type
      this.pid = pid
    },
    // 获取员工权限列表
    async getPermission() {
      // 发请求
      const { data } = await getPermissionListAPI()
      // 把列表式数据转为树形数据
      this.permissionList = listToTree(data, '0')
    },
    // 关闭弹框
    handleAddClose() {
      this.showAddDialog = false
    },
    // 新增权限
    async addPermissionForm() {
      // 校验
      // 发起新增请求
      await addPermissionFormAPI({
        ...this.permissionForm,
        type: this.type,
        pid: this.pid
      })
      // 关闭弹框
      this.handleAddClose()
      // 获取权限列表
      this.getPermission()
    },
    // 编辑弹框打开
    async handleOpenEdit(id) {
      // 打开弹框
      this.showEditDialog = true
      // 发起获取详情的请求
      const resp = await getPermissionDetailAPI(id)
      // 数据回显
      this.permissionForm = resp.data
    },
    // 编辑弹框关闭
    handleEditClose() {
      this.showEditDialog = false
    },
    // 编辑权限
    async updataPermissionForm() {
      // 建议在涉及调用接口时都用try-catch包，让代码更健壮
      try {
        // 校验
      // 发起编辑请求
        await updatePermissionFormAPI(this.permissionForm)
        // 关闭弹框
        this.handleEditClose()
        // 重新获取列表
        this.getPermission()
        this.$message.success('删除成功')
      } catch (err) {
        this.$message.error(err.message || '修改失败')
      }
    },
    // 删除权限
    async handleDel(id) {
      try {
        // 调接口发请求
        await delPermissionAPI(id)
        // 重新获取列表
        this.getPermission()
        this.$message.success('删除成功')
      } catch (err) {
        this.$message.error(err.message || '删除失败')
      }
    }
  }
}
</script>

<style>

</style>
